using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.IO;

namespace GameOfLife
{
    public class GOLController
    {

        public Dictionary<string, RuleSet> rulesets;
        RuleSet currentRuleset;
        string currentRulesetName;
        public Board currentBoard;
        GolGUI gui;

        public GOLController()
        {
            rulesets = new Dictionary<string,RuleSet>();

            currentRuleset = new RuleSet(new int[4] { 4, 6, 7, 8 }, new int[5] { 3,5,6,7,8 });
            currentRulesetName = "Surfice Tension";
            rulesets.Add(currentRulesetName,currentRuleset);
            rulesets.Add("Conway's rules", new RuleSet(new int[1] { 3 }, new int[2] { 2, 3 }));

            currentBoard = new Board(75,75);
            gui = new GolGUI(this,currentRulesetName);

            Application.Run(gui);
        }


        /// <summary>
        /// This method is called when the user wants
        ///  the cell matrix updated to the next time step
        /// 
        /// This method caused the cell matrix to progress
        ///  one step then causes the GUI to refresh
        /// </summary>
        public void OnStep()
        {
            currentBoard.Step(currentRuleset);
            gui.drawSimulation();
        }

        /// <summary>
        /// This method is called when the user wants a
        ///  random cell matrix
        /// 
        /// This method creates a random matrix then
        ///  causes the GUI to refresh
        /// </summary>
        public void OnRandom()
        {
            currentBoard.RandomBoard();
            gui.drawSimulation();
        }

        /// <summary>
        /// This method is called by the GUI
        ///  when the user wishes to display a
        ///  cell matrix that is saved in a file
        /// 
        /// This method loads the matrix from the
        ///  file and causes the GUI to refresh
        ///  when finished
        /// 
        /// </summary>
        /// <param name="filename">
        /// The fully qualified name of the file containing the
        ///  matrix that should be loaded
        /// </param>
        public void OnLoadMatrix(string filename)
        {
            currentBoard = new Board(filename);
            gui.drawSimulation();
        }


        /// <summary>
        /// This method is called by the GUI
        ///  when the user selects a new ruleset
        /// 
        /// This method changes the currentRulesetName
        ///  and currentRuleset to reflect the new ruleset
        /// </summary>
        /// <param name="rulename"> the name of the desired ruleset</param>
        public void OnRuleChange(string rulename)
        {
            currentRulesetName = rulename;
            currentRuleset = rulesets[rulename];
        }


    }
}
